package com.app.controller;

/*
  @author 阿伟
  @create 2020-11-24:0:43
  认证
*/

import com.app.dao.UserDao;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.entity.Code;
import com.common.entity.ResponseResult;
import com.common.entity.User;
import com.common.utils.AppJwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "登录认证")
@RestController
@RequestMapping("/auth")
//@CrossOrigin
public class AppAuthentication {

    @Autowired
    UserDao userDao;

    //登录验证  /{userName}/{password}
    @ApiOperation(value = "用户登录验证")
    @GetMapping("/login")
    public ResponseResult login(
            @ApiParam(name = "phone",value = "手机号",required = true)
            @RequestParam(value = "phone") String phone,
            @ApiParam(name = "password",value = "密码",required = true)
            @RequestParam(value = "password") String password){
        //查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_phone",phone);
        queryWrapper.eq("user_password",password);
        User user = userDao.selectOne(queryWrapper);
        if(user != null){
            //登录成功生成Apptoken
            String apptoken = AppJwtUtil.create(phone);
            return ResponseResult.success(Code.REQUEST_SUCCESS).Data("apptoken",apptoken);
        }else {
            return ResponseResult.error(Code.REQUEST_ERROR).Data("user",null);
        }
    }
}
